package com.closer.server.mapper.problem;

import com.closer.server.biz.submission.dto.SearchRequest;
import com.closer.server.biz.submission.dto.vo.SubmissionVO;
import com.closer.server.support.sql.QueryWrapperHelper;
import com.mybatisflex.core.BaseMapper;
import com.closer.server.biz.problem.entity.Submission;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;

/**
 * 提交记录表 映射层。
 *
 * @author closer
 */
public interface SubmissionMapper extends BaseMapper<Submission> {


    /**
     * 更新提交状态
     * @param id 提交记录ID
     * @param state 状态
     * @return 更新结果
     */
    default boolean updateState(Long id, String state){
        return UpdateChain.of(Submission.class)
                .set(Submission::getStatus, state)
                .where(Submission::getId).eq(id)
                .update();
    }

    /**
     * 更新结果
     * @param questionSubmitId 提交记录ID
     * @param JudgeInfo 判题信息
     * @return 提交结果
     */
    default boolean updateJudgeInfo(Long questionSubmitId, String JudgeInfo){
        return UpdateChain.of(Submission.class)
                .set(Submission::getJudgeInfo, JudgeInfo)
                .where(Submission::getId).eq(questionSubmitId)
                .update();
    }

    /**
     * 搜索提交记录
     * @param params 搜索参数
     * @return 提交记录
     */
    default Page<SubmissionVO> search(SearchRequest params){
        var queryWrapper = QueryWrapper.create()
                .select("submission.*","problem.title")
                .from("submission")
                .leftJoin("problem").on("submission.problem_id = problem.id")
                .orderBy("submission.create_time desc");
        QueryWrapperHelper.eqIfPresent(queryWrapper,params.getProblemId(), "submission.problem_id");
        QueryWrapperHelper.eqIfPresent(queryWrapper,params.getUserId(), "submission.user_id");
        QueryWrapperHelper.eqIfPresent(queryWrapper,params.getLanguage(), "submission.language");
        return paginateAs(params.getPageNumber(), params.getPageSize(),queryWrapper, SubmissionVO.class);
    }
}
